package com.hky.front_end_monitor.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hky.front_end_monitor.service.impl.SysErrorVitalServiceImpl;
import com.hky.front_end_monitor.service.impl.SysUserServiceImpl;
import com.hky.front_end_monitor.service.impl.SysUserVitalServiceImpl;
import com.hky.front_end_monitor.service.impl.SysWebVitalServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @auther Gpu is all you need
 * @date 2022/7/27
 */
@CrossOrigin
@RequestMapping("api/monitor")
@RestController
public class MonitorController {

    @Resource
    SysWebVitalServiceImpl sysWebVitalService;

    @Resource
    SysUserVitalServiceImpl sysUserVitalService;

    @Resource
    SysErrorVitalServiceImpl sysErrorVitalService;

    @Resource
    SysUserServiceImpl userService;

    @RequestMapping(value = "webVital")
    public Boolean receivePaint(@RequestBody String params) throws JsonProcessingException {
        Map map = new ObjectMapper().readValue(params,new TypeReference<Map<String,Object>>(){});
        if (!userService.isLegitimateUser(map)) return false;
        return sysWebVitalService.storeWebVital(map);
    }
    @RequestMapping("pv")
    public Boolean receivePv(@RequestBody String params) throws JsonProcessingException {
        Map map = new ObjectMapper().readValue(params,new TypeReference<Map<String,Object>>(){});
        if (!userService.isLegitimateUser(map)) return false;
        return sysUserVitalService.storeUserVital(map);
    }
    @RequestMapping("err")
    public Boolean receiveErr(@RequestBody String params) throws JsonProcessingException {
        Map map = new ObjectMapper().readValue(params,new TypeReference<Map<String,Object>>(){});
        if (!userService.isLegitimateUser(map)) return false;
        return sysErrorVitalService.storeErrorVital(map);
    }
}
